﻿/*
* Copyright 2009-2010 AKABANA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;

namespace Akabana.Amf.Util
{

    public class TypeResolver
    {
        protected static Dictionary<string, string> requestTypeMapping = new Dictionary<string,string>();

        protected static Dictionary<string, string> responseTypeMapping = new Dictionary<string,string>();


        public static string FindRequestTypeName(string typeName)
        {
            string result = typeName;

            if (requestTypeMapping.ContainsKey(typeName))
            {
                string mappingTypeName = null;
                requestTypeMapping.TryGetValue(typeName, out mappingTypeName);
                result = mappingTypeName;
            }

            return result;

        }

        public static string FindResponseTypeName(Type type)
        {
            return FindResponseTypeName(type.FullName);
        }

        public static string FindResponseTypeName(string typeName)
        {
            string result = typeName;

            if (responseTypeMapping.ContainsKey(typeName))
            {
                string mappingTypeName = null;
                responseTypeMapping.TryGetValue(result, out mappingTypeName);
                result = mappingTypeName;
            }

            return result;
        }

        public static void AddRequestType(string srcTypeName, string destTypeName)
        {
            requestTypeMapping.Add(srcTypeName,destTypeName);
        }

        public static void AddResponseType(string srcTypeName, string destTypeName)
        {
            requestTypeMapping.Add(srcTypeName, destTypeName);
        }

        public static void AddType(string srcTypeName, Type type)
        {
            if (requestTypeMapping.ContainsKey(srcTypeName))
            {
            }
            else
            {
                requestTypeMapping.Add(srcTypeName, type.FullName);
            }
            if (responseTypeMapping.ContainsKey(type.FullName))
            {
            }
            else
            {
                responseTypeMapping.Add(type.FullName, srcTypeName);
            }
        }
    }
}
